//axios 二次封装 
import axios from "axios";
import qs from "qs";

axios.defaults.baseURL = "http://localhost:8000";
axios.defaults.timeout = 5000;

function requestGet(url, params) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            url,
            headers: {},
            transformRequest: [],
            transformResponse: [
                function (data) {
                    return JSON.parse(data);
                }
            ],
            params,
        }).then((res) => {
            console.log(res);
            resolve(res);
        }).catch(err => {
            console.log(err);
            reject(err);
        })
    })
}

function requestPost(url, data) {
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded;chartset=utf-8"
            },
            transformRequest: [function (data) {
                // 序列化
                return qs.stringify(data);
            }],
            transformResponse: [],
            data
        }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        })
    })
}
export {
    requestGet,
    requestPost
}